package com.kzd.gtboon.main.home.adapter

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.*
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.gtboon.R
import com.kzd.gtboon.callback.AppItemDecoration
import com.kzd.gtboon.entity.*
import com.kzd.gtboon.main.*
import com.kzd.gtboon.main.game.adapter.*
import com.kzd.gtboon.main.home.*
import com.module.lemlin.image.loadRoundImage
import com.youth.banner.Banner

const val HOME_FEATURED_TYPE_2_N = -2
const val HOME_FEATURED_TYPE_1_N = -1
const val HOME_FEATURED_TYPE_0 = 0
const val HOME_FEATURED_TYPE_1 = 1
const val HOME_FEATURED_TYPE_2 = 2
const val HOME_FEATURED_TYPE_3 = 3
const val HOME_FEATURED_TYPE_4 = 4
const val HOME_FEATURED_TYPE_5 = 5
const val HOME_FEATURED_TYPE_10 = 10
const val HOME_FEATURED_TYPE_100 = 100

class HomeFeaturedAdapter : BaseMultiItemQuickAdapter<HomeIndex, BaseViewHolder>() {

    init {
        addItemType(HOME_FEATURED_TYPE_2_N, R.layout.view_home_type_2_n)
        addItemType(HOME_FEATURED_TYPE_1_N, R.layout.view_home_type_1_n)
        addItemType(HOME_FEATURED_TYPE_0, R.layout.view_home_type_0)
        addItemType(HOME_FEATURED_TYPE_1, R.layout.view_home_type_1)
        addItemType(HOME_FEATURED_TYPE_2, R.layout.view_home_type_2)
        addItemType(HOME_FEATURED_TYPE_3, R.layout.view_home_type_3)
        addItemType(HOME_FEATURED_TYPE_4, R.layout.view_home_type_4)
        addItemType(HOME_FEATURED_TYPE_5, R.layout.view_home_type_5)
        addItemType(HOME_FEATURED_TYPE_10, R.layout.view_home_type_10)
    }

    override fun convert(holder: BaseViewHolder, item: HomeIndex) {

        when (holder.itemViewType) {
            HOME_FEATURED_TYPE_2_N -> {
                val homeNavBoons = mutableListOf(
                    HomeNavBoon(icon = R.mipmap.ic_90038, name = "新游首发", type = 1),
                    HomeNavBoon(icon = R.mipmap.ic_90039, name = "免费当GM", type = 2),
                    HomeNavBoon(icon = R.mipmap.ic_90040, name = "免费领券", type = 3),
                    HomeNavBoon(icon = R.mipmap.ic_90041, name = "今日开服", type = 4),
                    HomeNavBoon(icon = R.mipmap.ic_90042, name = "游戏礼包", type = 5)
                )
                holder.getView<RecyclerView>(R.id.rvHomeType2NBody).apply {
                    layoutManager = LinearLayoutManager(context).apply {
                        orientation = LinearLayoutManager.HORIZONTAL
                    }
                    addItemDecoration(AppItemDecoration())
                }.adapter = HomeNavBoonAdapter().apply {
                    setNewInstance(homeNavBoons)
                    setOnItemClickListener { _, view, position ->
                        val itemData = getItem(position)
                        mOnItemHomeNav?.invoke(view, itemData)
                    }
                }
            }
            HOME_FEATURED_TYPE_1_N -> {
                holder.getView<RecyclerView>(R.id.rvHomeType1NBody)
                    .adapter = HomeAdvAdapter().apply {
                    setNewInstance(item.getConversionList(HomeAdv::class.java))
                }
            }
            HOME_FEATURED_TYPE_0 -> {
                holder.setText(R.id.tvHomeTypeTitle, item.title)
                    .getView<ImageView>(R.id.tvHomeType0Icon)
                    .loadRoundImage(
                        src = item.img ?: R.mipmap.ic_80003,
                        ConvertUtils.dp2px((10f)),
                        R.mipmap.ic_80003,
                        R.mipmap.ic_80003
                    )
            }
            HOME_FEATURED_TYPE_1 -> {
                holder.setText(R.id.tvHomeTypeTitle, item.title)
                holder.getView<RecyclerView>(R.id.rvHomeType1Body)
                    .adapter = Game2Adapter().apply {
                    setNewInstance(item.getConversionList(GameBean::class.java))
                }
            }
            HOME_FEATURED_TYPE_2 -> {
                holder.setText(R.id.tvHomeTypeTitle, item.title)
                holder.getView<RecyclerView>(R.id.rvHomeType2Body)
                    .adapter = Game1Adapter<GameBean>().apply {
                    setNewInstance(item.getConversionList(GameBean::class.java))
                }
            }
            HOME_FEATURED_TYPE_3 -> {
                holder.setText(R.id.tvHomeTypeTitle, item.title)
                holder.getView<RecyclerView>(R.id.rvHomeType3Body).apply {
                    layoutManager = LinearLayoutManager(context).apply {
                        orientation = LinearLayoutManager.HORIZONTAL
                    }
                }.adapter = Game5Adapter().apply {
                    setNewInstance(item.getConversionList(GameStart::class.java))
                }
            }
            HOME_FEATURED_TYPE_4 -> {
                holder.setText(R.id.tvHomeTypeTitle, item.title)
                val dataList = item.getConversionList(GameReserve::class.java)
                val bannerHoneType4Body = holder.getView<Banner<*, *>>(R.id.bannerHoneType4Body)
                bannerHoneType4Body.setOrientation(Banner.HORIZONTAL)
                    .setUserInputEnabled(true)
                    .setBannerGalleryEffect((10), (10), (10))
                    .adapter = Game6Adapter(dataList).apply {
                    mHomeType4Adapter = this
                    setOnItemReserve(object : Game6Adapter.OnItemReserve {
                        override fun onReserve(view: View, position: Int, reserve: GameReserve) {
                            mOnItemReserve?.invoke(view, position, reserve)
                        }
                    })
                }
                bannerHoneType4Body.setOnBannerListener { data, _ ->
                    if (data !is GameReserve)
                        return@setOnBannerListener
                    val gid = data.id
                        ?: return@setOnBannerListener
                    toGameDetailsActivity(gid)
                }

            }
            HOME_FEATURED_TYPE_5 -> {
                holder.setText(R.id.tvHomeTypeTitle, item.title)
                holder.getView<RecyclerView>(R.id.rvHomeType5Body).apply {
                    this.layoutManager = GridLayoutManager(context, 3)
                }.adapter = Game3Adapter().apply {
                    setNewInstance(item.getConversionList(GameBean::class.java))
                }
            }
            HOME_FEATURED_TYPE_10 -> {
                holder.setText(R.id.tvHomeTypeTitle, item.title)
                holder.getView<RecyclerView>(R.id.rvHomeType10Body).apply {
                    this.layoutManager = GridLayoutManager(context, 4)
                }.adapter = HomeSortAdapter().apply {
                    setNewInstance(item.getConversionList(HomeSort::class.java))
                }
            }
        }
    }

    private var mHomeType4Adapter: Game6Adapter? = null

    fun setDataHomeType4(position: Int, reserve: GameReserve) {
        mHomeType4Adapter?.setData(position, reserve)
    }

    private var mOnItemReserve: ((view: View, position: Int, reserve: GameReserve) -> Unit)? =
        null

    fun setOnItemReserve(listener: ((view: View, position: Int, reserve: GameReserve) -> Unit)) {
        this.mOnItemReserve = listener
    }

    private var mOnItemHomeNav: ((view: View, nav: HomeNavBoon) -> Unit)? = null

    fun setOnItemHomeNav(listener: ((view: View, nav: HomeNavBoon) -> Unit)) {
        this.mOnItemHomeNav = listener
    }
}

class HomeNavBoonAdapter : BaseQuickAdapter<HomeNavBoon, BaseViewHolder>(R.layout.item_home_nav) {

    override fun convert(holder: BaseViewHolder, item: HomeNavBoon) {
        holder.setText(R.id.tvHomeNavName, item.name)
            .getView<ImageView>(R.id.ivHomeNavIcon).setImageResource(item.icon)
    }
}

class HomeAdvAdapter :
    BaseQuickAdapter<HomeAdv, BaseViewHolder>(R.layout.item_home_adv) {
    override fun convert(holder: BaseViewHolder, item: HomeAdv) {
        holder.getView<ImageView>(R.id.ivHomeType1NIcon)
            .loadRoundImage(item.img, radius = ConvertUtils.dp2px(10f))
    }
}

class HomeSortAdapter :
    BaseQuickAdapter<HomeSort, BaseViewHolder>(R.layout.item_home_sort) {
    override fun convert(holder: BaseViewHolder, item: HomeSort) {
        holder.setText(R.id.tvHomeSortName, item.name)
    }
}
